﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;

namespace Shrestha.Charts.Graph
{
    class TriMarker : Shape
    {
        public double MarkerHeight{get;set;}
        public double MarkerWidth { get; set; }
        public double X { get; set; }
        public double Y { get; set; }
        public double YLat { get; set; }
        public double YLng { get; set; }

        private readonly TriGraph _tg;
        private Brush _tFill, _tStroke;
        
        public TriMarker(double xcoordinate, double ycoordinate, string Latitude, string Longitude, string EventDate, TriGraph tg)
        {
            X = xcoordinate; Y = ycoordinate; 
            this.ToolTip = "Latitude: " + Latitude + ", Longitude: " + Longitude + "\nDate of Event: " + EventDate;
            this._tg = tg;
        }

        public TriMarker() { }

        protected override Geometry DefiningGeometry
        {
            get
            {
                // Create a StreamGeometry for describing the shape
                StreamGeometry geometry = new StreamGeometry();
                geometry.FillRule = FillRule.EvenOdd;

                using (StreamGeometryContext context = geometry.Open())
                {
                    InternalTriMarkerGeometry(context);
                }

                // Freeze the geometry for performance benefits
                geometry.Freeze();

                return geometry;
            }
        }

        private void InternalTriMarkerGeometry(StreamGeometryContext context)
        {
            int mh = (int)(MarkerHeight / 2);
            int mw = (int)(MarkerWidth / 2);
            
            Point p1 = new Point(X - mw, Y - mh);
            Point p2 = new Point(X + mw, Y - mh);
            Point p3 = new Point(X + mw, Y + mh);
            Point p4 = new Point(X - mw, Y + mh);

            context.BeginFigure(p1, true, true);
            context.LineTo(p2, true, true);
            context.LineTo(p3, true, true);
            context.LineTo(p4, true, true);
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            _tStroke = this.Stroke;
            _tFill = this.Fill;
            this.StrokeThickness = 1.0;
            this.Stroke = Brushes.Blue;
            this.Fill = Brushes.SkyBlue;
            _tg.DrawTempLocation(YLat, YLng, X);
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            this.StrokeThickness = 1.0;
            this.Stroke = _tStroke;
            this.Fill = _tFill;
            _tg.ClearTempLocation();
            base.OnMouseLeave(e);
        }
    }
}
